package com.ctshk.rpc.order.goods.mapper;

import com.ctshk.rpc.order.goods.base.CustomBaseMapper;
import com.ctshk.rpc.order.goods.dto.OrderCommodityPageListDetailDTO;
import com.ctshk.rpc.order.goods.entity.OrderCommodity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 订单商品表 Mapper 接口
 * </p>
 *
 * @author 姜斌文
 * @since 2021-04-19
 */
public interface OrderCommodityMapper extends CustomBaseMapper<OrderCommodity> {

	@Select({
			"<script>",
			"SELECT",
			"t.id,t.order_id,t.commodity_id,t.commodity_name,t.commodity_number,t.commodity_picture_url,t.commodity_price," +
					"t.commodity_cost_attribute_id,t.commodity_cost_attribute_name,t.number,t.commodity_total_price," +
					"t.logistics_company_id, t.logistics_company_name, t.logistics_order_no, t.logistics_company_phone ",
			"FROM `ctshk-goods-order`.t_order_commodity t ",
			"WHERE t.is_deleted = 0 ",
			"<if test='orderIds != null and orderIds.size > 0'>",
			"AND t.order_id IN ",
			"<foreach collection='orderIds' item='id' open='(' separator=',' close=')'>",
			"#{id}",
			"</foreach>",
			"ORDER BY t.gmt_create DESC",
			"</if> ",
			"</script>"
	})
	List<OrderCommodityPageListDetailDTO> findCommodityDetailsByOrderId(@Param("orderIds") List<Long> orderIds);

	@Insert({
			"<script>" +
			"INSERT INTO `ctshk-goods-order`.`t_order_commodity` " +
					"<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">"+
					"<if test='id != null'>`id`, </if>" +
					"<if test='orderId != null'>`order_id`,</if>" +
					"<if test='commodityId != null'>`commodity_id`,</if>" +
					"<if test='commodityName != null'>`commodity_name`,</if>" +
					"<if test='commodityPictureUrl != null'>`commodity_picture_url`,</if>" +
					"<if test='commodityPrice != null'>`commodity_price`,</if>" +
					"<if test='commodityNumber != null'>`commodity_number`,</if>" +
					"<if test='commodityCostAttributeId != null'>`commodity_cost_attribute_id`,</if>" +
					"<if test='commodityCostAttributeName != null'>`commodity_cost_attribute_name`,</if>" +
					"<if test='number != null'>`number`,</if>" +
					"<if test='commodityTotalPrice != null'>`commodity_total_price`,</if>" +
					"<if test='logisticsCompanyId != null'>`logistics_company_id`,</if>" +
					"<if test='logisticsCompanyName != null'>`logistics_company_name`,</if>" +
					"<if test='logisticsOrderNo != null'>`logistics_order_no`,</if>" +
					"<if test='logisticsCompanyPhone != null'>`logistics_company_phone`,</if>" +
					"<if test='commoditySource != null'>`commodity_source`,</if>" +
					"<if test='effectiveTime != null'>`effective_time`,</if>" +
					"<if test='gmtCreate != null'>`gmt_create`,</if>" +
					"<if test='createId != null'>`create_id`,</if>" +
					"<if test='gmtModified != null'>`gmt_modified`,</if>" +
					"<if test='modifiedId != null'>`modified_id`,</if>" +
					"<if test='isDeleted != null'>`is_deleted`,</if>" +
					"</trim>" +
					"VALUES "+
					"<trim prefix=\"(\" suffix=\")\" suffixOverrides=\",\">" +
					"<if test='id != null'>#{id}, </if>" +
					"<if test='orderId != null'>#{orderId},</if>" +
					"<if test='commodityId != null'>#{commodityId},</if>" +
					"<if test='commodityName != null'>#{commodityName},</if>" +
					"<if test='commodityPictureUrl != null'>#{commodityPictureUrl},</if>" +
					"<if test='commodityPrice != null'>#{commodityPrice},</if>" +
					"<if test='commodityNumber != null'>#{commodityNumber},</if>" +
					"<if test='commodityCostAttributeId != null'>#{commodityCostAttributeId},</if>" +
					"<if test='commodityCostAttributeName != null'>#{commodityCostAttributeName},</if>" +
					"<if test='number != null'>#{number},</if>" +
					"<if test='commodityTotalPrice != null'>#{commodityTotalPrice},</if>" +
					"<if test='logisticsCompanyId != null'>#{logisticsCompanyId},</if>" +
					"<if test='logisticsCompanyName != null'>#{logisticsCompanyName},</if>" +
					"<if test='logisticsOrderNo != null'>#{logisticsOrderNo},</if>" +
					"<if test='logisticsCompanyPhone != null'>#{logisticsCompanyPhone},</if>" +
					"<if test='commoditySource != null'>#{commoditySource},</if>" +
					"<if test='effectiveTime != null'>#{effectiveTime},</if>" +
					"<if test='gmtCreate != null'>#{gmtCreate},</if>" +
					"<if test='createId != null'>#{createId},</if>" +
					"<if test='gmtModified != null'>#{gmtModified},</if>" +
					"<if test='modifiedId != null'>#{modifiedId},</if>" +
					"<if test='isDeleted != null'>#{isDeleted},</if>" +
					"</trim>"+
			"</script>"
	})
    void insertOrderCommodity(OrderCommodity orderCommodity);

	/**
	 * 订单id查询订单商品信息
	 * @param id
	 * @return
	 */
	@Select({
			"SELECT id,order_id,commodity_id,commodity_name,commodity_picture_url,commodity_price,commodity_number,commodity_cost_attribute_id,commodity_cost_attribute_name,number,commodity_total_price,logistics_company_id,logistics_company_name,logistics_order_no,logistics_company_phone,commodity_source,effective_time,create_id,modified_id,is_deleted,gmt_create,gmt_modified " +
					"FROM `ctshk-goods-order`.t_order_commodity " +
					"WHERE (order_id = #{orderId})"
	})
    OrderCommodity selectCommodityById(Long id);

	/**
	 * 查询订单商品
	 * @param id
	 * @return
	 */
	@Select({
			"SELECT id,order_id,commodity_id,commodity_name,commodity_picture_url,commodity_price,commodity_number," +
					"commodity_cost_attribute_id,commodity_cost_attribute_name,number,commodity_total_price," +
					"logistics_company_id,logistics_company_name,logistics_order_no,logistics_company_phone,commodity_source," +
					"effective_time,create_id,modified_id,is_deleted,gmt_create,gmt_modified " +
					"FROM `ctshk-goods-order`.t_order_commodity " +
					"WHERE (order_id = #{id})"
	})
    List<OrderCommodity> selectListByOrderId(@Param("id") Long id);

	@Select({
			"SELECT id,order_id,commodity_id,commodity_name,commodity_picture_url,commodity_price,commodity_number," +
					"commodity_cost_attribute_id,commodity_cost_attribute_name,number,commodity_total_price," +
					"logistics_company_id,logistics_company_name,logistics_order_no,logistics_company_phone,commodity_source," +
					"effective_time,create_id,modified_id,is_deleted,gmt_create,gmt_modified " +
					"FROM `ctshk-goods-order`.t_order_commodity " +
					"WHERE (order_id = #{id}) limit 1"
	})
	OrderCommodity selectByOrderId(@Param("id") Long id);
}
